import Vue from 'vue'
import VueRouter from 'vue-router'
import HomePC from '@/views/HomePC.vue'
import HomeM from '@/views/HomeM.vue'

Vue.use(VueRouter)

let isweb = () => {
  let u = navigator.userAgent;
  return !(u.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i));
};
let useComp = '';
if (isweb()) {
  useComp = '/pc';
} else {
  useComp = '/m';
}
console.log(isweb())
const routes = [
  {
    path: '/',
    redirect: useComp,
  },
  {
    path: '/pc',
    name: 'pc',
    component: HomePC,
  },
  {
    path: '/m',
    name: 'm',
    component: HomeM,
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (isweb() && to.name == 'm') {
    next(to.fullPath.replace(/\/m/i, '/pc'));
  } else if (!isweb() && to.name == 'pc') {
    next(to.fullPath.replace(/\/pc/i, '/m'));
  } else {
    next();
  }
})

export default router
